﻿using Forum.Infrastructure;
using Forum.Model.Entity;
using Forum.Model.Response;
using Forum.Repository.Interface;
using Forum.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Forum.Service.Service
{
    public class CommentService : ICommentService
    {
        private readonly IAppRepository<Comment> _commentRepo;

        public CommentService(IAppRepository<Comment> commentRepo)
        {
            _commentRepo = commentRepo;
        }

        public List<Comment> GetCommentById(string postId)
        {
            var list = _commentRepo.SugarQueryable.Where(a=> a.PostId == postId && a.ParentId == null).Includes(a => a.User).ToList();

            GetReplyList(list);

            return list;
        }

        private List<Comment>? GetReplyList(List<Comment> commontList)
        {
            if (commontList == null || commontList.Count == 0)
                return commontList;

            foreach (var item in commontList)
            {
                var list = _commentRepo.SugarQueryable.Where(a => a.ParentId == item.Id).Includes(a => a.User).ToList();
                item.Reply = GetReplyList(list);
            }
            return commontList;
        }
    }
}
